package org.jeecg.desen.cretab.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.desen.cretab.service.DesensHiveCreateTableService;
import org.jeecg.modules.meta.entity.SysTableinfo;
import org.jeecg.modules.meta.mapper.SysFieldinfoMapper;
import org.jeecg.modules.meta.service.MetaSynService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public abstract class AbstractHiveCreateService implements DesensHiveCreateTableService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    private String desc = "hive建表";

    @Autowired
    MetaSynService metaSynService;

    @Autowired
    SysFieldinfoMapper sysFieldinfoMapper;

    @Autowired
    @Qualifier("hiveDruidDataSource")
    DataSource druidDataSource;

    @Override
    public boolean createHiveTableForMeta(Date date) {
        if (verificationIsRun() == false) {
            return false;
        }

        metaSynService.startSyn(desc);
        boolean blean = createHiveTableForMetaPage(date);
        metaSynService.endSyn(desc);
        return blean;
    }

    /*根据所有元数据*/
    private boolean createHiveTableForMetaPage(Date date) {
        //获取所有元数据表 一千条一个批次
        for (int i = 1; i < 500; i++) {
            if (false == createHiveTableForMetaPage(i, 1000,date)) {
                break;
            }
        }
        //HiveTableInfo hiveTableInfo
        return true;
    }


    /*验证是否有脱敏线程正在处理*/
    private boolean verificationIsRun() {
        if (metaSynService.isSyn()) {
            return false;
        }

        return true;
    }


    protected void dropTable(SysTableinfo sysTableinfo){
        String sql = "drop table if exists " + schema + "." + sysTableinfo.getFdSchema() + "_" + sysTableinfo.getFdTablename();
        log.info("Running-dropTable: " + sql);
        execute(sql);
    }


    protected void createTableByLike(SysTableinfo sysTableinfo){
        //获取建表sql
        String createSql = getCreateSql(sysTableinfo);
        log.info("Running-createSql: " + createSql);
        execute(createSql);
    }


    private String getCreateSql(SysTableinfo sysTableinfo){
        String createSql = "CREATE TABLE IF NOT EXISTS " +
                schema + "." + sysTableinfo.getFdSchema() + "_" + sysTableinfo.getFdTablename() +
                " LIKE " + sysTableinfo.getFdSchema() + "." + sysTableinfo.getFdTablename() ;

        return createSql;
    }

    private boolean execute(String sql){
        try {
            return druidDataSource.getConnection().createStatement().execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            log.error("error-executehivesql" ,e);
            return false;
        }
    }

    protected ResultSet executeQuery(String sql){
        try {
            return druidDataSource.getConnection().createStatement().executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            log.error("error-executehivesql" ,e);
            return null;
        }
    }

    boolean createHiveTableForMetaPage(long current, long size,Date date) {
        IPage<SysTableinfo> page = new Page(current, size);
        //获取元数据平台表数据
        IPage<SysTableinfo> tableList = getTableList(page,date);
        if (null == tableList || tableList.getRecords().size() == 0) {
            return false;
        }
        //解析表
        analysisTable(tableList.getRecords());
        return true;
    }

    abstract IPage<SysTableinfo> getTableList(IPage page,Date date);

    abstract void analysisTable(List<SysTableinfo> sysTableinfos);

}
